package com.kung.qvs.ui.login;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import com.kung.qvs.data.JtRepository;
import com.kung.qvs.ui.main.DemoActivity;
import com.kung.qvs.ui.main.HomeActivity;
import com.kung.qvs.ui.main.HomeWorkerActivity;
import com.kung.qvs.utils.Constants;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * Created by goldze on 2017/7/17.
 */

public class LoginViewModel extends BaseViewModel<JtRepository> {
    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //密码的绑定
    public ObservableField<String> password = new ObservableField<>("");
    //用户名清除按钮的显示隐藏绑定
    public ObservableInt clearBtnVisibility = new ObservableInt();

    public ObservableField<Boolean> mKeepPwd = new ObservableField<>(false); //是否记住密码
    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();
        public SingleLiveEvent toPageForgetPwd = new SingleLiveEvent();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        model.saveKeepPwd(false);
    }

    public LoginViewModel(@NonNull Application application, JtRepository repository) {
        super(application, repository);
        //从本地取得数据绑定到View层
        userName.set(model.getUserName());
//        password.set(model.getPassword());
    }

    //清除用户名的点击事件, 逻辑从View层转换到ViewModel层
    public BindingCommand clearUserNameOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userName.set("");
        }
    });
    //密码显示开关  (你可以尝试着狂按这个按钮,会发现它有防多次点击的功能)
    public BindingCommand passwordShowSwitchOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //让观察者的数据改变,逻辑从ViewModel层转到View层，在View层的监听则会被调用
            uc.pSwitchEvent.setValue(uc.pSwitchEvent.getValue() == null || !uc.pSwitchEvent.getValue());
        }
    });
    //用户名输入框焦点改变的回调事件
    public BindingCommand<Boolean> onFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus) {
                clearBtnVisibility.set(View.VISIBLE);
            } else {
                clearBtnVisibility.set(View.INVISIBLE);
            }
        }
    });
    //登录按钮的点击事件
    public BindingCommand loginOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            login();
        }
    });

    public BindingCommand toPageForgetPwd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.toPageForgetPwd.call();
        }
    });

    //勾选用户协议
    public BindingCommand<Boolean> onCheckBoxChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean isChecked) {
            mKeepPwd.set(isChecked);
            model.saveKeepPwd(isChecked);
        }
    });

    /**
     * 网络模拟一个登陆操作
     **/
    private void login() {
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort("请输入密码！");
            return;
        }


        if (StringUtils.isTrimEmpty(model.getAccount())) {
            //第一次登录
            if ((!userName.get().equals(Constants.DEFAULT_ACCOUNT) && !userName.get().equals(Constants.DEFAULT_WORKER_ACCOUNT)) || !password.get().equals(Constants.DEFAULT_PWD)) {
                ToastUtils.showShort("用户名或者密码错误");
                return;
            }
        } else if (userName.get().equals(Constants.DEFAULT_WORKER_ACCOUNT) || model.getAccount().equals(Constants.DEFAULT_WORKER_ACCOUNT)) {
            if (!password.get().equals(Constants.DEFAULT_PWD)) {
                ToastUtils.showShort("用户名或者密码错误");
                return;
            }
        } else {
            if (!userName.get().equals(model.getAccount()) || !password.get().equals(model.getPassword())) {
                ToastUtils.showShort("用户名或者密码错误");
                return;
            }
        }

        if(userName.get().equals(Constants.DEFAULT_WORKER_ACCOUNT)) {
            //工作人员
            model.saveLoginType(Constants.LOGIN_TYPE_WORKER);
        } else {
            //管理员
            model.saveLoginType(Constants.LOGIN_TYPE_ADMIN);
        }

        //登录成功
        //保存账号密码
        model.saveAccount(userName.get());
        model.savePassword(password.get());
        model.saveLoginState(true);
        //进入DemoActivity页面
        if (model.getLoginType().equals(Constants.LOGIN_TYPE_WORKER)) {
            startActivity(HomeWorkerActivity.class);
        } else {
            startActivity(HomeActivity.class);
        }
        //关闭页面
        finish();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
